package com.pig4cloud.pig.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.item.api.bo.ItemBo;
import com.pig4cloud.pig.item.api.pojo.*;
import com.pig4cloud.pig.item.mapper.*;
import com.pig4cloud.pig.item.service.ItemService;
import com.pig4cloud.pig.item.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author pig code generator
 * @date 2020-10-13 10:09:07
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;


    @Override
    public ItemBo getItemBo(int id) {
        ItemBo itemBo = new ItemBo();
        Item item = itemMapper.selectById(id);

        itemBo.setBrand(item.getBrandId());
        itemBo.setId(item.getId());
        itemBo.setName(item.getName());
        itemBo.setPrice(item.getPrice());
        itemBo.setPrimaryClass(item.getOneId());
        itemBo.setSecondaryClass(item.getTwoId());
        itemBo.setTertiaryClass(item.getThreeId());

        return itemBo;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean saveItem(ItemBo itemBo) {

        Item item = new Item();

        //item.setId(itemBo.getId());
        item.setName(itemBo.getName());
        item.setPrice(itemBo.getPrice());
        item.setStatus(itemBo.getStatus());
        item.setTitle(itemBo.getTitle());
        item.setBrandId(itemBo.getBrand());
        item.setOneId(itemBo.getPrimaryClass());
        item.setTwoId(itemBo.getSecondaryClass());
        item.setThreeId(itemBo.getTertiaryClass());

        itemMapper.insert(item);

        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean updateItem(ItemBo itemBo) {

        Item item = itemMapper.selectById(itemBo.getId());

        item.setName(itemBo.getName());
        item.setPrice(itemBo.getPrice());
        item.setStatus(itemBo.getStatus());
        item.setTitle(itemBo.getTitle());
        item.setBrandId(itemBo.getBrand());
        item.setOneId(itemBo.getPrimaryClass());
        item.setTwoId(itemBo.getSecondaryClass());
        item.setThreeId(itemBo.getTertiaryClass());

        itemMapper.updateById(item);

        return true;
    }

//    public String getCategoryByName(long id) {
//        return categoryMapper.selectById(id).getCategoryName();
//    }
//
//    public Taskmark getTaskmarkByName(String taskmarkName) {
//        QueryWrapper<Taskmark> query = new QueryWrapper<>();
//        query.eq("taskmark_name", taskmarkName);
//        Taskmark taskmark = taskmarkMapper.selectOne(query);
//        return taskmark;
//    }
//
//    public Category getCategoryByName(String categoryName) {
//        QueryWrapper<Category> query = new QueryWrapper<>();
//        query.eq("category_name", categoryName);
//        Category category = categoryMapper.selectOne(query);
//        return category;
//    }
}
